package me.pplay.playtalk.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

import me.pplay.playtalk.callback.CropCallBack;
import me.pplay.playtalk.data.Globals;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.ArrayAdapter;

public class ImageCrop {
	private static final int PICK_FROM_CAMERA = 1;
	public static final int CROP_IMAGE = 2;
	private static final int PICK_FROM_FILE = 3;

	private Uri imageCaptureUri;
	private AlertDialog dialog = null;
	private Activity activity = null;
	
	private String filePath = "";
	
	
	public ImageCrop(Activity activity){
		this.activity = activity;
	}
	
	/**
	 * 取得选择框
	 * @return
	 */
	public AlertDialog getDialog(){
		if(dialog==null){
		    final String [] items			= new String [] {"拍照", "从相册选择"};				
			ArrayAdapter<String> adapter	= new ArrayAdapter<String> (activity, android.R.layout.select_dialog_item,items);
			AlertDialog.Builder builder		= new AlertDialog.Builder(activity);
			
			builder.setTitle("选择照片");
			builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
				public void onClick( DialogInterface dialog, int item ) { //pick from camera
					if (item == 0) {
						filePath 	  = "";
						Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
						filePath	  = Globals.FILE_PATH_IMAGE + System.currentTimeMillis() +".jpg";
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(filePath)));
                        activity.startActivityForResult(intent, PICK_FROM_CAMERA);
					} else { //pick from file
						Intent intent = new Intent(Intent.ACTION_PICK, null);
                        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                        activity.startActivityForResult(intent, PICK_FROM_FILE);
					}
				}
			} );
			
			dialog = builder.create();
		}
		return dialog;
	}


	public void startPhotoZoom() {
		if(imageCaptureUri == null)return;
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(imageCaptureUri, "image/*");
        intent.putExtra("crop", "true");
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪图片宽高
        intent.putExtra("outputX", 90);
        intent.putExtra("outputY", 90);
        intent.putExtra("return-data", true);
        activity.startActivityForResult(intent, CROP_IMAGE);
}
	
    /**
     * 用于回调的调用，放置在onActivityResult里面调用
     * @param requestCode
     * @param resultCode
     * @param data
     * @return
     * @throws IOException
     */
    public boolean capituredImage(int requestCode, int resultCode, Intent data, CropCallBack callback) throws IOException{
	    if (resultCode != Activity.RESULT_OK) return false;
	    switch (requestCode) {
		    case ImageCrop.PICK_FROM_CAMERA:
		    if(!filePath.equals(""))
		    	imageCaptureUri = Uri.fromFile(new File(filePath));
		    	startPhotoZoom();
		    	return true;
		    case ImageCrop.PICK_FROM_FILE: 
		    	imageCaptureUri = data.getData();
		    	startPhotoZoom();
		    	return true;
		    case ImageCrop.CROP_IMAGE:	
		    	// 处理结果
                Bundle extras = data.getExtras();
                if (extras != null) {
                        Bitmap photo = extras.getParcelable("data");
                        ByteArrayOutputStream stream = new ByteArrayOutputStream();
                        photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);// (0 - 100)压缩文件
                        callback.Callback(200, imageCaptureUri.getPath(), photo);
                }

		    	return true;
	    }
	    return false;
    }

}
